package com.liuxu.topnews.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import com.liuxu.topnews.MyApplication;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.liuxu.topnews.MyApplication.getContext;

public class AppUtils {
    private static boolean checkPackInfo(PackageManager var0, String var1) {
        boolean var2 = false;
        Object var3 = null;

        PackageInfo var5;
        try {
            var5 = var0.getPackageInfo(var1, 0);
        } catch (PackageManager.NameNotFoundException var4) {
            var4.printStackTrace();
            var5 = (PackageInfo)var3;
        }
        if (var5 != null) {
            var2 = true;
        }
        return var2;
    }
    public static Intent getAppOpenIntentByPackageName(Context var0, String var1) {
        var0.getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");
        intent.setFlags(270532608);
        intent.setComponent(new ComponentName(var1, "com.jifen.qkbase.main.MainActivity"));
        return intent;
    }
    public static List<PackageInfo> getInstallAppList() {
        List var2 = getContext().getPackageManager().getInstalledPackages(0);
        List<PackageInfo> PackageInfos=new ArrayList<>();
        for(int var0 = 0; var0 < var2.size(); ++var0) {
            PackageInfo var4 = (PackageInfo)var2.get(var0);
            int var1 = var4.applicationInfo.flags;
            ApplicationInfo var5 = var4.applicationInfo;
            if ((var1 & 1) <= 0) {
                PackageInfos.add(var4);
            }
        }
        return PackageInfos;
    }
    public static String getTopActivity() {
        Iterator var0 = ((ActivityManager)getContext().getSystemService("activity")).getRunningTasks(1).iterator();

        while(var0.hasNext()) {
            ActivityManager.RunningTaskInfo var3 = (ActivityManager.RunningTaskInfo)var0.next();
            String var1 = var3.topActivity.getShortClassName();
            String var2 = var3.topActivity.getClassName();
            String var4 = var3.topActivity.getPackageName();
            Log.e("shortClassName:", var1);
            Log.e("className:", var2);
            Log.e("packageName:", var4);
        }

        return "";
    }

    public static void goToMarket(String packageName) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + packageName));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            getContext().startActivity(intent);
        } catch (Exception var1) {
            var1.printStackTrace();
            Log.e("打开第三方下载失败:" ,var1.toString());
            Log.e("打开第三方下载失败","请自行下载");
        }
    }
}
